/*
 * @Author: 吴世扬 18368095041@163.com
 * @Date: 2024-04-19 10:48:07
 * @LastEditors: 吴世扬 18368095041@163.com
 * @LastEditTime: 2024-04-19 10:59:15
 * @FilePath: /cdss-admin-web/src/utils/string.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
export const numToCn = (num: number) => {
    if (num === 0) {
        return '零';
    }

    const units = ['', '十', '百', '千'];
    const chars = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
    const wanUnit = '万';

    let str = '';
    let zeroFlag = false; // 标记是否前面出现过非零数字
    let isWan = false; // 标记是否达到了万的级别

    while (num > 0) {
        const digit = num % 10;
        num = Math.floor(num / 10);

        if (digit === 0) {
            if (num > 0 && !zeroFlag) {
                str = chars[0] + str;
                zeroFlag = true;
            }
        } else {
            if (zeroFlag) {
                zeroFlag = false;
            }
            str = chars[digit] + units[str.length] + str;
            if (num === 0 && str.length === 4) {
                isWan = true;
                str = wanUnit + str;
            }
        }
    }

    // 去除开头的零
    if (str.startsWith(chars[0])) {
        str = str.slice(1);
    }

    return str;
};
